<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="报告类型" prop="reportType">
        <el-select v-model="queryParams.reportType" placeholder="请选择报告类型" clearable>
          <el-option v-for="item in reportTypeData" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
        <el-button plain icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="openReportType">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" stripe>
      <el-table-column label="图件类型名称" align="center" prop="mapType" />
      <el-table-column label="图件类型" align="center" prop="dictType" />
      <el-table-column label="报告类型" align="center" prop="reportType">
        <template #default="{ row }">
          <span v-if="row.reportType == '0'">预评估报告</span>
          <span v-else-if="row.reportType == '1'">现场调查报告</span>
        </template>
      </el-table-column>
      <el-table-column label="启用标识" align="center" prop="enableFlag">
        <template #default="{ row }">
          <span v-if="row.enableFlag == '0'">未启用</span>
          <span v-else-if="row.enableFlag == '1'">启用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="openReportType(row)">修改</el-button>
          <el-button type="text" @click="deleteReport(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination style="margin-top: 15px" v-show="total > 0" :total="total" v-model:page="queryParams.page"
      v-model:limit="queryParams.rows" @pagination="getList" />

    <el-dialog title="图件类型管理" v-model="reportTypeOpen" width="800px" append-to-body @close="proxy.resetForm('reportRef')">
      <el-form ref="reportRef" :model="reportForm" :rules="reportRules" label-width="120px">
        <el-form-item label="图件类型名称" prop="mapType">
          <el-input style="width: 100%;" v-model="reportForm.mapType" placeholder="请输入图件类型名称" />
        </el-form-item>
        <el-form-item label="图件类型" prop="dictCode">
          <el-select v-model="reportForm.dictCode" placeholder="请选择" @change="mapTypeDictChange">
            <el-option
              v-for="item in mapTypeOptions"
              :key="item.dictItemCode"
              :label="item.dictItemName"
              :value="item.dictItemCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报告类型" prop="reportType">
          <el-radio-group v-model="reportForm.reportType" class="ml-4">
            <el-radio label="0">预评估报告</el-radio>
            <el-radio label="1">现场调查报告</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="启用标识" prop="enableFlag">
          <el-radio-group v-model="reportForm.enableFlag" class="ml-4">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">未启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { queryMapTypes, saveMapType, checkTempById, deleteById, updateMapType, checkType, getDict, getById } from '@/api/resultManag/chartTypeManage.js'

const { proxy } = getCurrentInstance()
const data = reactive({
  queryParams: {
    page: 1,
    rows: 10,
    reportType: ''
  },
  total: 0,
  reportTypeData: [
    { label: '预评估报告', value: '0' },
    { label: '现场调查报告', value: '1' },
  ],
  tableData: [],
  reportForm: {
    mapType: '',
    reportType: '',
    enableFlag: '',
    dictCode: '',
    dictType: ''
  },
  mapTypeOptions: []
})
const { queryParams, total, reportTypeData, tableData, reportForm, mapTypeOptions } = toRefs(data)
const reportRules = ref({
  mapType: [{ required: true, message: '请填写图件类型名称！', trigger: 'blur' }],
  reportType: [{ required: true, message: '请选择报告类型！', trigger: 'change' }],
  enableFlag: [{ required: true, message: '请选择启用标识！', trigger: 'change' }]
})
const showSearch = ref(true)
const loading = ref(false)
const reportTypeOpen = ref(false)
// 分页
const getList = () => {
  loading.value = true
  queryMapTypes(queryParams.value).then(res => {
    if (res.code == 200) {
      tableData.value = res.data.rows
      total.value = res.data.total
    }
    loading.value = false
  }).catch(err => loading.value = false)
}
// 查询
const handleQuery = () => {
  queryParams.value.page = 1
  getList()
}
// 重置
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 新增/修改
const openReportType = (row) => {
  reportTypeOpen.value = true
  getMapTypeDict()
  row.id && getById(row.id).then(res => {
    reportForm.value = res.data
  })
}
// 获取图件类型字典项
const getMapTypeDict = () => {
  getDict('tjlx').then(res => {
    mapTypeOptions.value = res.data
  })
}
const mapTypeDictChange = (val) => {
  reportForm.value.dictType = mapTypeOptions.value.find(item => item.dictItemCode == val).dictItemName
}
const submitForm = () => {
  proxy.$refs.reportRef.validate(async (valid, fields) => {
    if (valid) {
      let res = await checkType(reportForm.value)
      if (res.code == 200) {
        if (res.data == false) {
          if (reportForm.value.id) {
            let saveRes = await updateMapType(reportForm.value)
            if (saveRes.code == 200) {
              proxy.$modal.msgSuccess('修改成功！')
              reportTypeOpen.value = false
              getList()
            }
          } else {
            let saveRes = await saveMapType(reportForm.value)
            if (saveRes.code == 200) {
              proxy.$modal.msgSuccess(saveRes.data)
              reportTypeOpen.value = false
              getList()
            }
          }
        } else {
          proxy.$modal.msgError('图件类型已存在！')
        }
      }
    } else {
      proxy.$refs.reportRef.scrollToField(Object.keys(fields)[0])
    }
  })
}
// 删除
const deleteReport = async (row) => {
  try {
    let res = await checkTempById(row.id)
    let tips = ''
    if (res.data) {
      tips = `${row.mapType}已上传模板，是否确认删除${row.mapType}`
    } else {
      tips = `是否确认删除${row.mapType}`
    }
    proxy.$modal.confirm(tips).then(() => {
      deleteById(row.id)
    }).then(() => {
      proxy.$modal.msgSuccess('删除成功！')
      getList()
    }).catch(() => {})
  } catch (error) {}
}

onMounted(() => {
  getList()
})

</script>